<?php
namespace app\index\controller;

use think\Controller;
use think\Validate;

class Member extends Common
{
    public $user;
    public function _initialize(){
        parent::_initialize();
        $this->user = session('user');
        $this->assign('user',$this->user);
    }
    
    public function index()
    {
        $this->checklogin();
        // $parent = ['id' => '0'];
        // $this->assign('parent', $parent);
        //分类
        $cates = Db('arctype')->where('status','1')->where('pid','0')->select();
        $avatar = Db('user_info')->where('uid',$this->user['id'])->value('avatar');
        $this->user['avatar'] = $avatar;
        $this->assign('user',$this->user);

        $this->assign('cates',$cates);
        return $this->fetch('information');
    }
      
    /**
     * 我的订单
     */
    public function order()
    {
        $this->checklogin();
        //分类
        $cates = Db('arctype')->where('status','1')->where('pid','0')->select();
        //订单
        $orders = Db('order')->alias('o')->join('archive a','o.pid=a.id')->where('o.uid',$this->user['id'])->field('o.*,a.title,a.description')->order('o.id desc')->select();

        $this->assign('orders',$orders);

        //$this->assign('page',$orders->render());
        $this->assign('cates',$cates);
        return $this->fetch();
    }
    
    /**
     * 我的积分
     */
    public function score()
    {
        $this->checklogin();
        //分类
        $cates = Db('arctype')->where('status','1')->where('pid','0')->select();
        $score = Db('long')->where(['uid'=>$this->user['id'],'type'=>2])->order('id desc')->select();

        //var_dump($score);die;
        foreach ($score as $key => $val) {
            $score[$key]['name'] = Db('user')->where('id',$val['uid'])->value('name');
        }
        $this->assign('cates',$cates);
        $this->assign('score',$score);
        return $this->fetch('points');
    }

    /**
     * 消费详情
     */
    public function long()
    {
        $this->checklogin();
        //分类
        $cates = Db('arctype')->where('status','1')->where('pid','0')->select();
        $money = Db('long')->where(['uid'=>$this->user['id'],'type'=>1])->order('id desc')->select();

        $this->assign('money',$money);
        $this->assign('cates',$cates);
        return $this->fetch();
    }
    
    /**
     * 修改资料
     */
    public function edit(){
        //return 1111;
        if ($this->request->isPost()) {
            $rule = [
                'name'  => 'require|max:25|min:1',
                'email' => 'email',
                'moblie'=> 'number|length:11',
                'idcard' => 'require|number|length:15,18',
            ];
            $msg = [
                'name.require' => '名称必须',
                'name.max'     => '名称最多不能超过25个字符',
                'name.min'     => '名称最少不能少于1个字符',
                'email'        => '邮箱格式错误',
                'moblie.number' => '手机号码格式不正确',
                'moblie.length' => '手机号码格式不正确',
                'idcard.require' => '身份证号码不能为空',
                'idcard.number ' => '身份证号码必须为数字',
                'idcard.length' => '身份证号码必须为15到18位数字',
            ];

            $data = $this->request->param();
            $validate = new Validate($rule,$msg);

            $result = $validate->check($data);
            if (!$result) {
                return json(['status'=>-1,'info'=>$validate->getError()]);
            }
            if ($data['avatar']) {
                $avatar = $data['avatar'];
                unset($data['avatar']);
            }
            $result = Db('user')->where('id',$this->user['id'])->update($data);
            $rs2 = Db('user_info')->where('uid',$this->user['id'])->update(['avatar'=>$avatar]);
            if ($result!==false) {
                session('user',array_merge($this->user,$data));
                return json(['status'=>1,'info'=>'更新成功']);
            }
            return json(['status'=>-1,'info'=>'更新失败']);
        }
    }


    /**
     * 删除订单
     */
    public function del(){
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            $result = Db('order')->where('id',$id)->delete();
            if ($result!==false) {
                return json(['status'=>1,'info'=>'删除订单成功']);
            }
            return json(['status'=>-1,'info'=>'删除订单失败']);
        }
    }


    public function upload(){
        $file = request()->file('files');
         if($file[0]){
                $info = $file[0]->move(ROOT_PATH . 'uploads' . DS . 'avatar');
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 jpg
                    // echo $info->getExtension();
                    // // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    // echo $info->getSaveName();
                    // // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    // echo $info->getFilename();
                    return json(['status'=>1,'url'=>'/uploads' . DS . 'avatar'.DS.date('Ymd').DS.$info->getFilename()]);
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }
        //var_dump($image);die;
    }

    public function subordinate(){
        $this->checklogin();
        //分类
        $users = Db('user')->where(['pid'=>$this->user['id'],'user_type'=>1])->select();
        //$score = Db('long')->where(['uid'=>$this->user['id'],'type'=>2])->order('id desc')->select();
        $ids = array_column($users, 'id');
        $long = Db('long')->alias('l')->join('tf_user u','u.id=l.uid')->where(['l.uid'=>['in',$ids],'l.type'=>1,'l.in_or_out'=>2])->field('l.*,u.name')->select();
        
        $this->assign('users',$users);
        $this->assign('long',$long);
        return $this->fetch();
    }
}
